package com.efeichong.common.config;

import com.efeichong.audit.DefaultValueProvider;
import com.efeichong.common.domain.entity.redis.LoginUser;
import com.efeichong.common.security.LoginContextHolder;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.project.domain.dao.db.SysUserDao;
import com.efeichong.project.domain.entity.db.SysUser;
import com.efeichong.util.FastJpaSpringUtils;

/**
 * @author lxk
 * @date 2020/12/18
 * @description 用户初始化审计人信息(创建人 / 更新人)
 */
public class AuditorHolder implements DefaultValueProvider<SysUser> {


    @Override
    public SysUser getValue() {
        SysUserDao userDao = FastJpaSpringUtils.getBean(SysUserDao.class);
        LoginUser loginUser = LoginContextHolder.getLoginUserOrElseThrow();
        if (loginUser != null) {
            SysUser sysUser = userDao.selectById(loginUser.getId()).orElseThrow(OptionEmptyFunction.create());
            return sysUser;
        }
        return null;
    }
}
